package com.zbkj.admin.model.vo;

import com.zbkj.dal.base.enums.*;
import com.zbkj.dal.entity.ba.BizImg;
import com.zbkj.dal.entity.ba.EbSupplyOrderOperateLogEntity;
import com.zbkj.dal.entity.ba.Share;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
public class MerchantShareDetailResponse {

    @ApiModelProperty("供享订单编号")
    private String orderNo;

    @ApiModelProperty("用户信息")
    private UserInfo userInfo;

    /**
     * @see com.zbkj.dal.base.enums.BizSharedOrderStatus
     */
    @ApiModelProperty("订单状态")
    private String status;

    @ApiModelProperty("订单状态")
    private String statusText;

    /**
     * @see com.zbkj.dal.base.enums.BizSharedOrderStatus.SubStatus
     */
    @ApiModelProperty("订单子状态")
    private String subStatus;

    @ApiModelProperty("订单子状态")
    private String subStatusText;

    /**
     * @see com.zbkj.dal.base.enums.BizShareModeEnum
     */
    @ApiModelProperty("供享模式")
    private String shareMode;

    @ApiModelProperty("供享模式名称")
    private String shareModeName;

    // ------------------- 用户提交信息 -------------------
    @ApiModelProperty("用户提交的信息")
    private Detail userSubmit;
    @ApiModelProperty("用户提交备注")
    private String description;

    // ------------------- 初评结果信息 -------------------
    @ApiModelProperty("初评结果的信息")
    private Detail initialEvaluation;
    @ApiModelProperty("初评结果 1通过 0不通过")
    private Integer estimateStatus;
    @ApiModelProperty("(初评之后才有)初步估价区间-起始")
    private String estimatePriceStart;
    @ApiModelProperty("(初评之后才有)初步估价区间-结束")
    private String estimatePriceEnd;
    @ApiModelProperty("初评结果的瑕疵与污渍")
    private String estimateRemark;

    // ------------------- 最终结果信息 -------------------
    @ApiModelProperty("终评结果 1通过 0不通过")
    private Integer finalEstimationStatus;
    @ApiModelProperty("最终估价")
    private String finalEstimationPrice;
    @ApiModelProperty("雅识")
    private String finalEstimationBalance;
    @ApiModelProperty("终评瑕疵&污渍描述")
    private String finalEstimationRemark;

    // ------------------- 图片信息 -------------------
    @ApiModelProperty("图片信息")
    private Map<RecyclePositionEnums, BizImg> pics;

    // ------------------- 补充信息 -------------------

    // 其他补充信息可能不同阶段会出现不同的信息

    @ApiModelProperty("初评送货信息")
    private DeliveredInfo deliveredInfo;

    @ApiModelProperty("商品归还信息")
    private DeliveredInfo returnedInfo;

    // ------------------- 日志信息 -------------------
    @ApiModelProperty("日志信息")
    private List<EbSupplyOrderOperateLogEntity> operateLog;

    @Data
    public static class UserInfo {
        @ApiModelProperty("用户ID")
        private Integer userId;

        @ApiModelProperty("用户名称")
        private String userName;

        @ApiModelProperty("用户手机号")
        private String phone;

        @ApiModelProperty("用户等级")
        private String userLevel;
    }


    @Data
    public static class Detail {

        @ApiModelProperty("品类")
        private String productType;

        @ApiModelProperty("品牌")
        private String brand;

        @ApiModelProperty("型号/编码")
        private String code;

        @ApiModelProperty("成色")
        private String quality;

        @ApiModelProperty("配件")
        private String parts;
    }

    @Data
    @ApiModel("预约取件信息")
    public static class PickupAppt {

        @ApiModelProperty(value = "快递名称", example = "顺丰速递")
        private String deliveryName;

        @ApiModelProperty("快递单号")
        private String expressNumber;

        @ApiModelProperty(value = "寄件人地址", example = "上海市**区")
        private String senderAddress;

        @ApiModelProperty(value = "取件时间", notes = "做为退回信息的时候没有这个参数")
        private LocalDateTime pickupTime;
    }

    @Data
    @ApiModel("上门取件")
    public static class InStoreAppt {

        @ApiModelProperty(value = "预约上门时间", notes = "做为退回信息的时候没有这个参数")
        private LocalDateTime pickupTime;

        @ApiModelProperty("地址")
        private String merchantAddress;
    }

    @Data
    @ApiModel("供享收货信息")
    public static class DeliveredInfo {

        @ApiModelProperty("收货方式 express:物流 pickup:上门")
        private String deliveryType;

        @ApiModelProperty("物流预约信息")
        private PickupAppt expressAppt;

        @ApiModelProperty("上门预约信息")
        private InStoreAppt pickupAppt;
    }

    @Data
    @ApiModel("供享退回信息")
    public static class returnedInfo {

        @ApiModelProperty("退回方式 express:物流 pickup:上门")
        private String returnedType;

        @ApiModelProperty("物流预约信息")
        private PickupAppt expressAppt;

        @ApiModelProperty("上门预约信息")
        private InStoreAppt pickupAppt;
    }

    public static MerchantShareDetailResponse buildByShare(Share share, Map<RecyclePositionEnums, BizImg> bizImgs) {
        MerchantShareDetailResponse shareDetailResponse = new MerchantShareDetailResponse();
        // 订单信息
        shareDetailResponse.setOrderNo(share.getOrderNo());
        shareDetailResponse.setShareMode(share.getShareMode());
        shareDetailResponse.setShareModeName(BizShareModeEnum.toName(share.getShareMode()));
        shareDetailResponse.setDescription(share.getDescription());
        shareDetailResponse.setEstimatePriceStart(share.getEstimatePriceStart());
        shareDetailResponse.setEstimatePriceEnd(share.getEstimatePriceEnd());
        shareDetailResponse.setStatus(share.getStatus());
        shareDetailResponse.setStatusText(BizSharedOrderStatus.text(share.getStatus()));
        shareDetailResponse.setSubStatus(share.getSubStatus());
        shareDetailResponse.setSubStatusText(BizSharedOrderStatus.SubStatus.text(share.getSubStatus()));

        // 用户提交信息
        Detail userSubmit = new Detail();
        userSubmit.setProductType(ProductTypeEnums.valueOf(share.getProductType()).getDesc());
//        userSubmit.setBrand(share.getBrand());
        userSubmit.setCode(share.getCode());
        userSubmit.setQuality(QualityEnum.valueOf(share.getQuality()).getDesc());
        userSubmit.setParts(Arrays.stream(share.getParts().split(",")).map(p -> ProductParts.valueOf(p).getDesc()).collect(Collectors.joining()));
        shareDetailResponse.setUserSubmit(userSubmit);
        // 初评信息
        Detail initialEvaluation = new Detail();
        if (StringUtils.isNotEmpty(share.getProductTypeFinal())) {
            initialEvaluation.setProductType(ProductTypeEnums.valueOf(share.getProductTypeFinal()).getDesc());
        }
//        initialEvaluation.setBrand(share.getBrandFinal());
        initialEvaluation.setCode(share.getCodeFinal());
        if (StringUtils.isNotEmpty(share.getQualityFinal())) {
            initialEvaluation.setQuality(QualityEnum.valueOf(share.getQualityFinal()).getDesc());
        }
        if (StringUtils.isNotEmpty(share.getPartsFinal())) {
            initialEvaluation.setParts(Arrays.stream(share.getPartsFinal().split(",")).map(p -> ProductParts.valueOf(p).getDesc()).collect(Collectors.joining(",")));
        }
        shareDetailResponse.setEstimatePriceStart(share.getEstimatePriceStart());
        shareDetailResponse.setEstimatePriceEnd(share.getEstimatePriceEnd());
        shareDetailResponse.setInitialEvaluation(initialEvaluation);
        shareDetailResponse.setEstimateRemark(share.getRemark());
        // 图片信息
        shareDetailResponse.setPics(bizImgs);
        return shareDetailResponse;
    }
}
